<template>
    <!-- <h2>注册页面</h2> -->
    <el-form :model="form" :rules="rules" ref="regForm" class="login" label-width="90px">
        <h2>用户注册</h2>
        <br>
        <el-form-item label="新账号:" prop="username">
            <el-input ref="username" v-model="form.username"  prefix-icon="el-icon-user" placeholder="请输入用户名">
            </el-input>
        </el-form-item>
        <el-form-item label="新密码:"  prop="password">
            <el-input ref="password" type="password" v-model="form.password" prefix-icon="el-icon-lock"  show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确定密码:"  prop="chkpassword">
            <el-input ref="chkpassword" type="password" v-model="form.chkpassword" prefix-icon="el-icon-lock"  show-password placeholder="再次输入密码"></el-input>
        </el-form-item>
        <el-form-item class="login-btns">
            <el-button type="danger" @click="register">注册</el-button>
            <el-button class="login-btns-1" type="primary" @click="$router.push('/login')">登录</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
export default {
  data () {
    // 验证用户名
    const checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('输入新用户名'))
      } else if (value.lenght < 3 || value.lenght > 10) {
        return callback(new Error('请输入3到10个字符串'))
      } else if (!/^[a-zA-Z]\w+$/.test(value)) {
        return callback(new Error('请输入有效用户名'))
      } else {
        callback()
      }
    }
    // 验证密码是否有效
    const checkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('输入新密码'))
      } else if (value.lenght < 6) {
        return callback(new Error('请输入0-6个字符串'))
      } else {
        callback()
      }
    }
    // 再次验证密码是否与上次输入密码
    const cheCkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('输入新密码'))
      } else if (value !== this.form.password) {
        return callback(new Error('两次输入密码不一样'))
      } else {
        callback()
      }
    }
    return {
      form: {},
      rules: {
        username: { validator: checkUsername },
        password: { validator: checkPassword },
        chkpassword: { validator: cheCkPassword }
      }
    }
  },
  methods: {
    register () {
      this.$refs.regForm.validate(valid => {
        if (valid) {
          this.$http.post('admin/register', { ...this.form }).then(res => {
            if (res.err > 0) {
              this.$message.error(res.desc)
              if (res.err === 101) {
                this.$refs.username.clear()
                this.$refs.username.focus()
              }
            } else {
              this.$notify({ title: '恭喜！注册成功', type: 'success' })
              this.$router.push('/login')
            }
          })
        }
      })
    }

  },
  mounted () {
    this.$refs.username.focus()
    // 敲回车提交
    window.onkeydown = (event) => {
      if (event.keyCode === 13) this.register()
    }
  }
}
</script>
<style>
.login-btns{
    margin: center;
    margin-top: 20px;
    /* background-color: red; */
}
</style>

<style lang="less" scopend>
.login{
    position: fixed;
    width: 340px;
    left: 50%;
    top: 40%;
    transform: translate(-50%,-50%);
    background-color: rgb(178, 190, 190);
    box-shadow: 0 0 16px rgba(0,0,0,.08);
    border-radius: 8px;
    padding: 20px 30px;
    h2{
        text-align: center;
        font-size: 24px;
        margin-bottom: 20px;
        color: black;
    }
}
</style>
